﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using AMicroblogAPI;
using AMicroblogAPI.Common;
using AMicroblogAPI.DataContract;
using System.IO;

namespace AMicroblogAPIv2_Test
{
    [TestClass]
    public class FunctionTest
    {
        public FunctionTest()
        {
            var jsonStr = "{\"access_token\":\"2.00nAjAXC0daABx7cb726f1287uYBiC\",\"expires_in\":86400,\"uid\":\"2320057781\"}";
            
            var token = JsonSerializationHelper.JsonToObject<OAuthAccessToken>(jsonStr);

            AMicroblogAPI.Environment.AccessToken = token;
        }

        [TestMethod]
        public void TestJsonSerialize()
        {
            var jsonStr = "{\"access_token\":\"2.00nAjAXC0daABx7cb726f1287uYBiC\",\"expires_in\":86400,\"uid\":\"2320057781\"}";

            var token = JsonSerializationHelper.JsonToObject<OAuthAccessToken>(jsonStr);
            Assert.IsTrue(token != null);
        }

        [TestMethod]
        public void TestGetAuthorizationUri()
        {
            var authUri = AMicroblog.GetAuthorizationUri(AMicroblogAPI.Environment.RedirectUri, "code", "abc", "mobile");

            Assert.IsTrue(authUri.StartsWith(APIUri.Authorize));

            // https://api.weibo.com/oauth2/authorize?client_id=872044423&redirect_uri=http%3a%2f%2famicroblog.35free.net%2fAuthorizationCallback.html&response_type=code&state=abc&display=mobile#
        }

        [TestMethod]
        public void TestGetAccessTokenByPassword()
        {
            // If the appKey does not pass the verification, this call would fail with error: 21326 unauthorized_client
            var accessToken = AMicroblog.GetAccessTokenByPassword("amicroblogtest@sina.com", "amicroblogtest");

            Assert.IsNotNull(accessToken);
        }

        [TestMethod]
        public void TestGetAccessTokenByAuthorizationCode()
        {
            var code = string.Empty; // TODO: auth code required.
            var accessToken = AMicroblog.GetAccessTokenByAuthorizationCode(code, AMicroblogAPI.Environment.RedirectUri);

            Assert.IsNotNull(accessToken);
        }

        [TestMethod]
        public void TestGetUserInfo()
        {
            var userInfo = AMicroblog.GetUserInfo(long.Parse(AMicroblogAPI.Environment.AccessToken.UserID));

            Assert.IsNotNull(userInfo);
        }

        [TestMethod]
        public void TestGetStatus()
        {
            var userInfo = AMicroblog.GetUserInfo(long.Parse(AMicroblogAPI.Environment.AccessToken.UserID));
            Assert.IsNotNull(userInfo);

            var statusInfo = AMicroblog.GetStatus(userInfo.LatestStatus.ID);

            Assert.IsNotNull(statusInfo);
        }

        [TestMethod]
        public void TestPost()
        {
            var stat = "Test" + new Random().Next().ToString();
            var statusInfo = AMicroblog.PostStatus(new UpdateStatusInfo() { Status = stat });

            Assert.IsNotNull(statusInfo);
            Assert.AreEqual(stat, statusInfo.Text);

            var count = AMicroblog.GetCountersOfCommentNForward(new long[]{ statusInfo.ID});
            Assert.IsNotNull(count);

        }

        [TestMethod]
        public void TestGetTrends()
        {
            var trends = AMicroblog.GetHourTrends();

            Assert.IsNotNull(trends);

            trends = AMicroblog.GetDayTrends();

            Assert.IsNotNull(trends);

            trends = AMicroblog.GetWeekTrends();

            Assert.IsNotNull(trends);

            try
            {
                var userTrends = AMicroblog.GetUserTrends(long.Parse(AMicroblogAPI.Environment.AccessToken.UserID));
                Assert.IsNotNull(userTrends);

                if (userTrends.Count > 0)
                {
                    var trend = userTrends[0];
                    var result = AMicroblog.UnfollowTrend(trend.ID);
                    if (result)
                    {
                        AMicroblog.FollowTrend(trend.HotWord);
                    }
                }
            }
            catch (AMicroblogException)
            {
                // 10025
            }
        }

        [TestMethod]
        public void TestGetFollowers()
        {
            var followers = AMicroblog.GetFollowers(long.Parse(AMicroblogAPI.Environment.AccessToken.UserID));

            Assert.IsNotNull(followers);
        }

        [TestMethod]
        public void TestGetFriends()
        {
            var friends = AMicroblog.GetFriends(long.Parse(AMicroblogAPI.Environment.AccessToken.UserID));

            Assert.IsNotNull(friends);
        }

        [TestMethod]
        public void TestGetUserStatuses()
        {
            var statuses = AMicroblog.GetUserStatuses(long.Parse(AMicroblogAPI.Environment.AccessToken.UserID));

            Assert.IsNotNull(statuses);
        }

        [TestMethod]
        public void TestGetFriendsStatuses()
        {
            var statuses = AMicroblog.GetFriendsStatuses();

            Assert.IsNotNull(statuses);
        }

        [TestMethod]
        public void TestGetCommentsByMe()
        {
            var comments = AMicroblog.GetCommentsByMe();

            Assert.IsNotNull(comments);

            comments = AMicroblog.GetCommentsToMe();

            Assert.IsNotNull(comments);
        }

        [TestMethod]
        public void TestGetUnreadInfo()
        {
            var unread = AMicroblog.GetUnreadInfo(long.Parse(AMicroblogAPI.Environment.AccessToken.UserID));

            Assert.IsNotNull(unread);
        }

        [TestMethod]
        public void TestGetPrivacy()
        {
            var privacyInfo = AMicroblog.GetPrivacy();

            Assert.IsNotNull(privacyInfo);
        }

        [TestMethod]
        public void TestGetFavorites()
        {
            var result = AMicroblog.GetFavorites();

            Assert.IsNotNull(result);
        }

        [TestMethod]
        public void TestGetEmotions()
        {
            var emotions = AMicroblog.GetEmotions();

            Assert.IsNotNull(emotions);
        }

        [TestMethod]
        public void TestGetUserSearchSuggestions()
        {
            var sug = AMicroblog.GetUserSearchSuggestions("A");

            Assert.IsNotNull(sug);
        }

        [TestMethod]
        public void TestFollow()
        {
            var result = AMicroblog.Follow("SINA-Open-API-CSharp");

            Assert.IsNotNull(result);

            result = AMicroblog.Unfollow("SINA-Open-API-CSharp");

            Assert.IsNotNull(result);
        }

        [TestMethod]
        public void TestGetFriendshipInfo()
        {
            var result = AMicroblog.GetFriendshipInfo("Aldrick","SINA-Open-API-CSharp");

            Assert.IsNotNull(result);
        }

        [TestMethod]
        public void TestGetFollowerUserIDs()
        {
            var result = AMicroblog.GetFollowerUserIDs("Aldrick");

            Assert.IsNotNull(result);

            result = AMicroblog.GetFollowingUserIDs("Aldrick");

            Assert.IsNotNull(result);
        }

        [TestMethod]
        public void TestGetTags()
        {
            var result = AMicroblog.GetTags(long.Parse(AMicroblogAPI.Environment.AccessToken.UserID));

            Assert.IsNotNull(result);

            result = AMicroblog.GetSuggestedTags();

            Assert.IsNotNull(result);

            var tagIDs = AMicroblog.CreateTags("API", "SDK");

            Assert.IsNotNull(tagIDs);
        }

        [TestMethod]
        public void TestGetRateLimitStatus()
        {
            var result = AMicroblog.GetRateLimitStatus();

            Assert.IsNotNull(result);
        }
    }
}
